/*******************************************************************************
 * Copyright (c) 2005, 2014 springside.github.io
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 *******************************************************************************/
package com.yihu.wlyy.service.common.account;

import com.yihu.wlyy.entity.WlyyRole;
import com.yihu.wlyy.entity.WlyyRoleRealm;
import com.yihu.wlyy.repository.RoleAreaDao;
import com.yihu.wlyy.repository.RoleDao;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springside.modules.utils.Clock;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


/**
 * 医生角色管理类.
 */
@Service("doctorRoleService")
@Transactional(rollbackFor = Exception.class)
public class RoleService extends TokenService {

    @Autowired
    private RoleDao roleDao;

    @Autowired
    private RoleAreaDao roleAreaDao;

    private Clock clock = Clock.DEFAULT;

    /**
     * 获取用户角色列表
     */
    public List<WlyyRole> getUserRole(String user) {
        return roleDao.findUserRole(user);
    }

    /**
     * 区域去末尾零
     */
    private String getShortArea(String area) {
        char strs[] = area.toCharArray();// 将字符串转化成字符数组
        String last = "";
        for (int i = area.length() - 1; i >= 0; i--) {
            if ('0' == strs[i]) {
                last += "0";
            } else {
                break;
            }
        }
        if (last.length() > 0) {
            return area.substring(0, area.lastIndexOf(last));
        } else {
            return area;
        }
    }

    /**
     * 获取角色区域
     */
    public List<Map<String, String>> getUserRoleAndArea(String user) {
        List<Map<String, String>> re = new ArrayList<>();
        List<WlyyRole> roleList = roleDao.findUserRole(user);

        if (roleList != null && roleList.size() > 0) {
            //遍历角色
            for (WlyyRole role : roleList) {
                Map<String, String> map = new HashMap<>();
                map.put("code", role.getCode());
                map.put("name", role.getName());


                //获取区域权限
                String areaString = "";
                List<WlyyRoleRealm> areaList = roleAreaDao.findByRoleCode(role.getCode());
                if (areaList != null && areaList.size() > 0) {
                    //获取最大权限区域
                    List<String> areas = new ArrayList<>();
                    List<String> orgs = new ArrayList<>();
                    for (WlyyRoleRealm areaItem : areaList) {
                        String currentArea = areaItem.getAreaCode();
                        int currentLength = currentArea.length();

                        if (currentLength == 6) //区域
                        {
                            areas.add(currentArea);
                        } else {               //机构
                            orgs.add(currentArea);
                        }
                    }

                    //区域权限重复校验
                    for (int i = areas.size() - 1; i >= 0; i--) {
                        String currentArea = areas.get(i);
                        for (int j = 0; j < areas.size() - i - 1; j++) {
                            String shortArea = getShortArea(areas.get(j));
                            if (currentArea.startsWith(shortArea))   //包含则移除
                            {
                                areas.remove(i);
                            }
                        }
                    }
                    //机构权限重复校验
                    for (int m = orgs.size() - 1; m >= 0; m--) {
                        String currentOrg = orgs.get(m);
                        for (int n = 0; n < areas.size() - m - 1; n++) {
                            String shortArea = getShortArea(areas.get(n));
                            if (currentOrg.startsWith(shortArea))   //包含则移除
                            {
                                orgs.remove(m);
                            }
                        }
                    }

                    for (String item : areas) {
                        areaString += item + ",";
                    }
                    for (String item : orgs) {
                        areaString += item + ",";
                    }
                    areaString = areaString.substring(0, areaString.length() - 1);
                }
                map.put("areas", areaString);
                re.add(map);
            }
        }

        return re;
    }
}
